<template>
  <div class="app-container">
    <div class="filter-container">
      <div class="search-container">
        <el-input v-model="searchQuery.title" placeholder="请输入内容" style="width: 240px;margin-right:10px;" class="filter-item" @keyup.enter.native="handleFilter" />
        <el-date-picker
          v-model="select_month"
          type="month"
          placeholder="选择月或周"
          style="width: 120px;margin-right:1rem;"
          :clearable="true"
          :picker-options="pickerOptions"
          @change="handleSelectMonth"
        />
        <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
          查询
        </el-button>
      </div>
      <pagination v-show="total>0" :total="total" :page.sync="searchQuery.pageNo" :layout="'total, sizes, prev, pager, next, jumper'" :limit.sync="searchQuery.pageSize" @pagination="getManageDatas" />
    </div>

    <el-table
      :key="tableKey"
      v-loading="pageLoading"
      :data="listData"
      border
      fit
      style="width: 100%;"
    >
      <el-table-column label="成员" align="center">
        <template slot-scope="{row}">
          <span>{{ row.nickname }}</span>
        </template>
      </el-table-column>
      <el-table-column label="任务" align="center">
        <template slot-scope="{row}">
          <span>{{ row.task.done }} / {{ row.task.total }}</span>
        </template>
      </el-table-column>
      <el-table-column label="任务系数（没做）" align="center">
        <template slot-scope="{row}">
          <span>{{ row.weight.total }}</span>
        </template>
      </el-table-column>
      <el-table-column label="需求" align="center">
        <template slot-scope="{row}">
          <span>{{ row.demand.done }} / {{ row.demand.total }}</span>
        </template>
      </el-table-column>
      <el-table-column label="缺陷" align="center">
        <template slot-scope="{row}">
          <span>{{ row.defect.done }} / {{ row.defect.total }}</span>
        </template>
      </el-table-column>
      <el-table-column label="逾期" align="center">
        <template slot-scope="{row}">
          <span>{{ row.overdue }}</span>
        </template>
      </el-table-column>
      <el-table-column label="核查" align="center">
        <template slot-scope="{row}">
          <span>{{ row.check.done }} / {{ row.check.total }}</span>
        </template>
      </el-table-column>
    </el-table>

  </div>
</template>

<script>
import { dealWithMonthDay, dealWithWeekDay } from '@/utils/index'
import Pagination from '@/components/Pagination'
import * as ManageApi from '@/api/manage'
let week = false
let weekText = ''
const dateMonth = new Date()
dateMonth.setMonth(dateMonth.getMonth())
dateMonth.setDate(1)

export default {
  name: 'Member',
  components: { Pagination },
  data() {
    return {
      tableKey: 0,
      total: 0,
      pageLoading: false,
      select_month: dateMonth,
      weekText: '',
      pickerOptions: {
        shortcuts: [{
          text: '本周',
          onClick(picker) {
            week = true
            weekText = this.text
            const date = new Date()
            date.setTime(date.getTime() - 3600 * 1000 * 24 * (date.getDay() - 1))
            picker.$emit('pick', date)
            setTimeout(() => {
              week = false
            }, 0)
          }
        }, {
          text: '上周',
          onClick(picker) {
            week = true
            weekText = this.text
            const date = new Date()
            date.setTime(date.getTime() - 3600 * 1000 * 24 * (date.getDay() + 6))
            picker.$emit('pick', date)
            setTimeout(() => {
              week = false
            }, 0)
          }
        }]
      },
      searchQuery: {
        pageNo: 1,
        pageSize: 10,
        start_time: '',
        end_time: ''
      },
      listData: []
    }
  },
  mounted() {
    this.handleSelectMonth(this.select_month)
  },
  methods: {
    async getManageDatas(pageQuery) {
      if (pageQuery) {
        this.searchQuery = Object.assign(this.searchQuery, pageQuery)
      }
      this.listData = []
      this.pageLoading = true
      const { data } = await ManageApi.getManageMembers(this.searchQuery)
      if (data && data.total) {
        this.listData = data.items
        this.total = data.total
      }
      this.pageLoading = false
    },
    handleFilter() {
      this.searchQuery.pageNo = 1
      this.getManageDatas()
    },
    handleSelectMonth(val) {
      if (val) {
        this.weekText = week ? weekText : ''
        const { startTime, endTime } = week ? dealWithWeekDay(val) : dealWithMonthDay(val)
        this.searchQuery['start_time'] = startTime
        this.searchQuery['end_time'] = endTime
        this.searchQuery.pageNo = 1
        this.getManageDatas()
      }
    }
  }
}
</script>

<style scoped>
.filter-container {
  display: flex;
  justify-content: space-between;
}
.pagination {
  display: flex;
  justify-content: center;
  margin-top: 1rem;
}
.search-container {
  display: flex;
  align-items: flex-start;
}
</style>
